//
//  CCCommonSystemView.m
//  CCFC_IPHONE
//
//  Created by  xuchen(陈旭)， xichen(陈曦)， qq：511272827 on 10-11-27.
//  Copyright 2010 cc_team. All rights reserved.
//
#include "CCAll.h"

#ifdef	OBJC_MACRO
#import <Foundation/Foundation.h>
#import "CCCommonSystemView.h"
#import "CCSimulator.h"

// 调用系统短信界面
MFMessageComposeViewController *showSystemSMSView(id delegate, 
												  const NSArray *recipientArr, 
												  const NSString *textBody, 
												  BOOL animated)
{
	assert(recipientArr != nil && textBody != nil);
	
	if(![MFMessageComposeViewController canSendText])
	{
		return nil;
	}
	
	MFMessageComposeViewController *picker = OBJ_INIT(MFMessageComposeViewController);
	if(!picker)
	{
		return nil;
	}
	
	picker.messageComposeDelegate = delegate;
	
	picker.recipients = [NSArray arrayWithArray:(NSArray *)recipientArr];
	picker.body = (NSString *)textBody;
	
	//显示新短信View
	[delegate presentModalViewController:picker animated:animated];
	[picker release];
	return picker;
}

// 调用联系人界面
ABPeoplePickerNavigationController *showAddressBookPeopleView(id delegate, 
															  ABPropertyID propertyId, 
															  BOOL animated)
{
	ABPeoplePickerNavigationController *peoplePicker = OBJ_INIT(ABPeoplePickerNavigationController);
	if(!peoplePicker)
	{
		return nil;
	}
	[peoplePicker setPeoplePickerDelegate:delegate];

	//设置过滤属性
	[peoplePicker setDisplayedProperties:
		[NSArray arrayWithObject:[NSNumber numberWithInt:propertyId]]];

	//显示联系人界面
	[delegate presentModalViewController:peoplePicker animated:animated];
	[peoplePicker release];
	return peoplePicker;
}

// 调用系统mail界面
MFMailComposeViewController *showMailView(id delegate, 
										  const NSArray *recipients, 
										  const NSArray *ccRecipients, 
										  const NSString *subject, 
										  const NSString *textBody, 
										  BOOL isHTML,
										  BOOL animated)
{
	assert(subject != nil && textBody != nil);
	
	if (![MFMailComposeViewController canSendMail]) 
	{ 
		// 调用系统配置mail界面, 不传入收件人和抄送人信息
		NSString *email = [NSString stringWithFormat:
						   @"mailto:&subject=%@&body=%@", subject, textBody]; 
		email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
		[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
		return nil;
	}

	MFMailComposeViewController *mailViewController = OBJ_INIT(MFMailComposeViewController);
	mailViewController.mailComposeDelegate = delegate;
	[mailViewController setToRecipients:(NSArray *)recipients];
	[mailViewController setCcRecipients:(NSArray *)ccRecipients];
	[mailViewController setSubject:(NSString *)subject];
	[mailViewController setMessageBody:(NSString *)textBody isHTML:isHTML];
	
	[delegate presentModalViewController:mailViewController animated:animated];
	[mailViewController release];
	return mailViewController;
}

// 调用系统播放器播放资源界面
MPMediaPickerController *showIpodPlayResourceView(id delegate,
												  MPMediaType mediaType, 
												  const NSString *prompt, 
												  BOOL isAllowMultiSelected, 
												  BOOL animated)
{
	assert(prompt != nil);
	
#ifdef MACRO_SIMULATOR
	return nil;			// 模拟器直接返回nil
#else
	MPMediaPickerController *playlistView = 
	[[MPMediaPickerController alloc] initWithMediaTypes:mediaType];
	if(!playlistView)
	{
		return nil;
	}
	
	playlistView.delegate = delegate;
	playlistView.prompt = (NSString *)prompt;
	playlistView.allowsPickingMultipleItems = isAllowMultiSelected;
	
	[delegate presentModalViewController:playlistView animated:animated];
	[playlistView release];
	return playlistView;
#endif
}

// 调用系统照片库
UIImagePickerController *showImagePickerView(id delegate,
											 UIImagePickerControllerSourceType imgType,
											 BOOL animated)
{
	UIImagePickerController *imgPickerView = OBJ_INIT(UIImagePickerController);
	if(!imgPickerView)
	{
		return nil;
	}
	imgPickerView.delegate = delegate;
	imgPickerView.sourceType = imgType;
	
	[delegate presentModalViewController:imgPickerView animated:animated];
	RELEASE(imgPickerView);
	
	return imgPickerView;
}

@implementation CCCommonSystemView

@end

#endif	//OBJC_MACRO
